/**
 * @file spin_lock.h
 * @author your name (you@domain.com)
 * @brief 自旋锁
 * @version 0.1
 * @date 2024-02-29
 *
 * @copyright Copyright (c) 2024
 *
 */

#pragma once

#include <atomic>

namespace wheels {

class SpinLock {
   public:
    SpinLock() : flag_(false) {}
    SpinLock(const SpinLock &) = delete;
    SpinLock &operator=(const SpinLock &) = delete;

    void lock() {
        while (!flag_.test_and_set())
            ;
    }

    void unlock() { flag_.clear(); }

   private:
    std::atomic_flag flag_;
};

}  // namespace wheels
